package by.bsuir.sn.controller
{
	import by.bsuir.sn.model.MainModel;

	import by.bsuir.sn.view.popup.ObjectCreatePopup;

	import flash.display.DisplayObject;
	import flash.profiler.profile;

	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.managers.PopUpManager;
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;

	public class MainController
	{
		private static var _inst : MainController;
		private var _service : RemoteObject;
		private var model : MainModel = MainModel.inst;

		private var popup : ObjectCreatePopup;

		/****************************************************/
		public function MainController(lock : SingletonLock)
		{
		}

		public static function get inst() : MainController
		{
			if(!_inst)
				_inst = new MainController(new SingletonLock());
			return _inst;
		}

		/****************************************************/

		public function get service() : RemoteObject
		{
			return _service;
		}

		public function set service(value : RemoteObject) : void
		{
			_service = value;
		}

		public function getAllTables() : void
		{
			if(_service)
			{
				model.executing = true;
				var res : AsyncToken = _service.getAll();
				res.addResponder(new Responder(onGetAllTables, onServiceFault));
			}
		}

		public function addItem(item : Object) : void
		{
			if(_service)
			{
				model.executing = true;
				var res : AsyncToken = _service.addItem(item);
				res.addResponder(new Responder(onAddItem, onServiceFault));
			}
		}

		public function deleteItem(item : Object) : void
		{
			if(_service)
			{
				model.executing = true;
				var res : AsyncToken = _service.deleteItem(item);
				res.addResponder(new Responder(onDeleteItem, onServiceFault));
			}
		}

		public function updateItem(item : Object) : void
		{
			if(_service)
			{
				model.executing = true;
				var res : AsyncToken = _service.updateItem(item);
				res.addResponder(new Responder(onUpdateItem, onServiceFault));
			}
		}

		public function callAddItemPopup(item : Object) : void
		{
			model.currentCreatedItem = item;
			if(!popup)
				popup = PopUpManager.createPopUp(FlexGlobals.topLevelApplication as DisplayObject,
						ObjectCreatePopup, true) as ObjectCreatePopup;
			popup.visible = true;
			PopUpManager.bringToFront(popup);
			PopUpManager.centerPopUp(popup);
		}

		private function onAddItem(event : ResultEvent) : void
		{
			model.executing = false;
			getAllTables();
		}

		private function onDeleteItem(event : ResultEvent) : void
		{
			model.executing = false;
			getAllTables();
		}

		private function onUpdateItem(event : ResultEvent) : void
		{
			model.executing = false;
			getAllTables();
		}

		public function onGetAllTables(event : ResultEvent) : void
		{
			model.allTables = event.result as ArrayCollection;
			model.executing = false;
		}

		public function onServiceFault(event : FaultEvent) : void
		{
			trace(event.fault);
			Alert.show(event.fault.faultString);
			model.executing = false;
		}
	}
}
class SingletonLock
{

}
